package com.wuuxiang.polestar.demo.util;

import java.io.File;
import java.io.IOException;

import org.apache.curator.test.TestingServer;

/**********************************************************
 * 类名称：ZookeeperServer <br>
 * 类描述： 内嵌版Zookeeper,测试服务先启动他<br>
 * 创建人：liuwei <br>
 * 创建时间：2017年12月22日下午1:40:51 <br>
 * 修改备注： <br>
 **********************************************************/
public class ZookeeperServer {

	private static TestingServer localServer;

	public static void start(final int port) {
		try {
			localServer = new TestingServer(port,
					new File(String.format("target/test_zk_data/%s/", System.nanoTime())));
			// CHECKSTYLE:OFF
		} catch (final Exception ex) {
			// CHECKSTYLE:ON
			ex.printStackTrace();
		} finally {
			Runtime.getRuntime().addShutdownHook(new Thread() {

				@Override
				public void run() {
					try {
						Thread.sleep(1000L);
						localServer.close();
					} catch (final InterruptedException | IOException ex) {
					}
				}
			});
		}
	}
}
